package org.bm.blaise.testing.plane;

/*
 * TestPlaneVisometry.java
 *
 * Created on Jul 30, 2009, 3:15:03 PM
 */


import org.bm.blaise.specto.plane.diffeq.PlaneVectorField;
import org.bm.blaise.specto.plane.diffeq.*;
import data.propertysheet.PropertySheet;
import java.awt.geom.Point2D;
import org.apache.commons.math.FunctionEvaluationException;
import org.apache.commons.math.analysis.*;
import org.bm.blaise.sequor.timer.BetterTimeClock;
import org.bm.blaise.specto.plane.*;
import org.bm.blaise.specto.plane.diffeq.PlaneCobwebFunction;
import org.bm.blaise.specto.plane.function.PlaneFunctionGraph;
import org.bm.blaise.specto.visometry.Plottable;
import scio.diffeq.DifferenceEquation1;

/**
 *
 * @author ae3263
 */
public class TestDifferentialEquations extends javax.swing.JFrame {

    BetterTimeClock timer;

    /** Creates new form TestPlaneVisometry */
    public TestDifferentialEquations() {
        timer = new BetterTimeClock();
        initComponents();

        timerBar1.setTimer(timer);

        // FUNCTIONS

        MultivariateVectorialFunction func1 =
                new MultivariateVectorialFunction() {
                    public double[] value(double[] point) throws FunctionEvaluationException, IllegalArgumentException {
                        return new double[]{1, (.5 + point[1]) * (point[1] - 1)};
                    }
                };
        MultivariateRealFunction func2 = new MultivariateRealFunction() {
                    public double value(double[] point) throws FunctionEvaluationException, IllegalArgumentException {
                        return (.5 + point[1]) * (point[1] - 1);
                    }
                };
        MultivariateVectorialFunction func3 =
                new MultivariateVectorialFunction() {
                    public double[] value(double[] point) throws FunctionEvaluationException, IllegalArgumentException {
                        return new double[]{point[1], .1};
                    }
                };
        MultivariateRealFunction func4 =
                new MultivariateRealFunction() {
                    public double value(double[] point) throws FunctionEvaluationException, IllegalArgumentException {
                        return point[2]; // point is (t, x, y), so this is saying x' = y.
                    }
                };
        MultivariateRealFunction func5 =
                new MultivariateRealFunction() {
                    public double value(double[] point) throws FunctionEvaluationException, IllegalArgumentException {
                        return 0.1;
                    }
                };


        // DIFF EQ'S

        DEPlot.addPlottable(new PlaneAxes());
        DEPlot.addPlottable(new PlaneVectorField(func1, DEPlot.getPlotSampleSetGenerator()));
        DEPlot.addPlottable(new PlaneDESolution(new Point2D.Double(-1.5, .5), func2, 5.0));
        DEPlot.setTimeClock(timer);

        DEPlot2.addPlottable(new PlaneAxes());
        DEPlot2.addPlottable(new PlaneVectorField(func3, DEPlot2.getPlotSampleSetGenerator()));
        DEPlot2.addPlottable(new PlaneDE2Solution(new Point2D.Double(0.2, -0.2), func4, func5, 0.0, 10.0));

        UnivariateRealFunction f = new UnivariateRealFunction() {
            public double value(double x) throws FunctionEvaluationException {
                return 1-(x-.5)*(x-.5);
            } };

        CobwebPlot.addPlottable(new PlaneAxes());
        CobwebPlot.addPlottable(new PlaneFunctionGraph(new UnivariateRealFunction(){public double value(double x){return x;}}));
        CobwebPlot.addPlottable(new PlaneFunctionGraph(f));
        CobwebPlot.addPlottable( new PlaneCobwebFunction( new Point2D.Double(), new DifferenceEquation1(f, 0, 20) ));
        CobwebPlot.setDesiredRange(-1.0, -1.0, 2.0, 1.5);

        
        // PANELS

        data.propertysheet.editor.EditorRegistration.registerEditors();
        rollupPanel1.add("Timer", new PropertySheet(timer));
        rollupPanel1.add("Visometry", new PropertySheet(DEPlot.getVisometry()));
        for (Plottable p : DEPlot.getPlottables()) {
            rollupPanel1.add(p.toString(), new PropertySheet(p));
        }
//        rollupPanel1.add("Point Style", new PropertySheet(planePlotComponent1.getVisometryGraphics().getPointStyle()));
//        rollupPanel1.add("Stroke Style", new PropertySheet(planePlotComponent1.getVisometryGraphics().getPathStyle()));
//        rollupPanel1.add("Text Style", new PropertySheet(planePlotComponent1.getVisometryGraphics().getTextStyle()));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        rollupPanel1 = new gui.RollupPanel();
        tabPane = new javax.swing.JTabbedPane();
        DEPlot = new org.bm.blaise.specto.plane.PlanePlotComponent();
        DEPlot2 = new org.bm.blaise.specto.plane.PlanePlotComponent();
        CobwebPlot = new org.bm.blaise.specto.plane.PlanePlotComponent();
        timerBar1 = new org.bm.blaise.sequor.timer.TimerBar();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(0, 0, 0));

        jScrollPane1.setViewportView(rollupPanel1);

        getContentPane().add(jScrollPane1, java.awt.BorderLayout.EAST);

        tabPane.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                tabPaneStateChanged(evt);
            }
        });

        org.jdesktop.layout.GroupLayout DEPlotLayout = new org.jdesktop.layout.GroupLayout(DEPlot);
        DEPlot.setLayout(DEPlotLayout);
        DEPlotLayout.setHorizontalGroup(
            DEPlotLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 567, Short.MAX_VALUE)
        );
        DEPlotLayout.setVerticalGroup(
            DEPlotLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 370, Short.MAX_VALUE)
        );

        tabPane.addTab("1-Var DE", DEPlot);

        org.jdesktop.layout.GroupLayout DEPlot2Layout = new org.jdesktop.layout.GroupLayout(DEPlot2);
        DEPlot2.setLayout(DEPlot2Layout);
        DEPlot2Layout.setHorizontalGroup(
            DEPlot2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 567, Short.MAX_VALUE)
        );
        DEPlot2Layout.setVerticalGroup(
            DEPlot2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 370, Short.MAX_VALUE)
        );

        tabPane.addTab("2-Var DE", DEPlot2);

        org.jdesktop.layout.GroupLayout CobwebPlotLayout = new org.jdesktop.layout.GroupLayout(CobwebPlot);
        CobwebPlot.setLayout(CobwebPlotLayout);
        CobwebPlotLayout.setHorizontalGroup(
            CobwebPlotLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 567, Short.MAX_VALUE)
        );
        CobwebPlotLayout.setVerticalGroup(
            CobwebPlotLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 370, Short.MAX_VALUE)
        );

        tabPane.addTab("Cobwebbing", CobwebPlot);

        getContentPane().add(tabPane, java.awt.BorderLayout.CENTER);

        timerBar1.setRollover(true);
        getContentPane().add(timerBar1, java.awt.BorderLayout.PAGE_START);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tabPaneStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_tabPaneStateChanged
        rollupPanel1.removeAll();
        PlanePlotComponent ppc = (PlanePlotComponent) tabPane.getSelectedComponent();
        rollupPanel1.add("Timer", new PropertySheet(timer));
        rollupPanel1.add("Visometry", new PropertySheet(ppc.getVisometry()));
        for (Plottable p : ppc.getPlottables()) {
            rollupPanel1.add(p.toString(), new PropertySheet(p));
        }
}//GEN-LAST:event_tabPaneStateChanged

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new TestDifferentialEquations().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.bm.blaise.specto.plane.PlanePlotComponent CobwebPlot;
    private org.bm.blaise.specto.plane.PlanePlotComponent DEPlot;
    private org.bm.blaise.specto.plane.PlanePlotComponent DEPlot2;
    private javax.swing.JScrollPane jScrollPane1;
    private gui.RollupPanel rollupPanel1;
    private javax.swing.JTabbedPane tabPane;
    private org.bm.blaise.sequor.timer.TimerBar timerBar1;
    // End of variables declaration//GEN-END:variables
}
